package com.keshan.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author keshan
 * @date 2022/10/27 - 17:07
 */
public class DateUtil {
    /**
     * string格式转化为date格式
     * @param datestr
     * @return
     */
    public static Date StringToDate(String datestr){
        Date date = null;
        try {
            date = new SimpleDateFormat("yyyyMMddHHmmss").parse(datestr);
        } catch (
                ParseException e) {
            e.printStackTrace();
        }
        return date;
    }


    public static Boolean IsTimeToday(Date dateFromLibrary)
    {
        Date now =new Date();
        String dateNow = new SimpleDateFormat("yyyy-MM-dd").format(now);
        String dateFromLibraryStr = new SimpleDateFormat("yyyy-MM-dd").format(dateFromLibrary);
        return dateNow.equals(dateFromLibraryStr);
    }

    public static List<Date> getStartAndEndToday()
    {
        List<Date> dateList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date date = calendar.getTime();
        dateList.add(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 59);
        date = calendar.getTime();
        dateList.add(date);
        return dateList;
    }
    //getStartAndEndMonth  变成前后两个月的了
    public static List<Date> getStartAndEndTwoMonth()
    {

        List<Date> dateList = new ArrayList<>();
        Calendar calendar1 = Calendar.getInstance();
        calendar1.set(Calendar.HOUR_OF_DAY,0);
        calendar1.set(Calendar.MINUTE, 0);
        calendar1.set(Calendar.SECOND, 0);
        calendar1.set(Calendar.MILLISECOND, 0);
        calendar1.set(Calendar.MONTH, calendar1.get(Calendar.MONTH)-1);
        calendar1.set(Calendar.YEAR, calendar1.get(Calendar.YEAR));
        calendar1.set(Calendar.DAY_OF_MONTH,1);
        Date date = calendar1.getTime();
        dateList.add(date);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.set(Calendar.HOUR_OF_DAY, 23);
        calendar2.set(Calendar.MINUTE, 59);
        calendar2.set(Calendar.SECOND, 59);
        calendar2.set(Calendar.MILLISECOND, 59);
        calendar2.set(Calendar.DATE,calendar2.getActualMaximum(Calendar.DATE));
        calendar2.set(Calendar.MONTH, calendar2.get(Calendar.MONTH));
        calendar2.set(Calendar.YEAR, calendar2.get(Calendar.YEAR));
        date = calendar2.getTime();
        dateList.add(date);
        return dateList;
    }
    public static List<Date> getStartAndEndMonth()
    {
        List<Date> dateList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH));
        calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
        calendar.set(Calendar.DAY_OF_MONTH,1);
        Date date = calendar.getTime();
        dateList.add(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 59);
        calendar.set(Calendar.MONTH, 10);
        calendar.set(Calendar.DATE,calendar.getActualMaximum(Calendar.DATE));
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH));
        calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
        date = calendar.getTime();
        dateList.add(date);
        return dateList;
    }

}
